// for index.html

var index;
if (!index){
    index = {};
}

// set background image
index.setBackgroundImage = function(){
    console.log('set background image');
    $.ajax({
        url: '/plus/bing_image',
        type: 'get',
        success: function(data, text_status){
            var content = JSON.parse(data);
            var image = content.images[0];
            var url = image.url;
            // check the url
            if (url.slice(0,4) != 'http'){
                url = 'http://s.en.bing.net' + url;
            }
            var details = image.hs;
            for (var i=0; i<details.length; i++){
                var detail = details[i];
                var desc = detail.desc;
                var left = detail.locx + '%';
                var top = detail.locy + '%';
                // onmouseover="index.showImageDetail(this)" onmouseout="index.hideImageDetail(this)"
                $('.background_layout').append('<div class="image_detail" title="'+desc+
                    '"  style="left:'
                    +left+';top:'+top+';">'+desc+'</div>')
            }
            $('.background_layout').css('background-image', 'url('+url+')');
        }
    });
}

index.infoMouseEnter= function(self){
    $('#avatar').animate({
       top: '0px'
    }, 1000);
    $('.avatar_mask').animate({
        top: '-166px'
    }, 1000);
}

index.infoMouseLeave = function(self){
    $('#avatar').animate({
       top: '166px'
    }, 1000);
    $('.avatar_mask').animate({
        top: '0px'
    }, 1000);
}

$(document).ready(function(){
    index.setBackgroundImage();

    $('.background_layout').on('mouseenter', '.image_detail', function(e){
        $(this).animate({
            width: '160px',
            height: '80px'
        });
        e.preventDefault();
        return false;
    });

    $('.background_layout').on('mouseleave', '.image_detail', function(e){
        $(this).animate({
            width: '22px',
            height: '22px'
        });
        e.preventDefault();
        return false;
    });

});

